home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1354 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Returning a reference
  5. Date: 10 Jan 1996 17:04:55 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan10120455@g7240065.bridge.bst.bls.com>
  8. References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: 100754.2730@compuserve.com's message of Wed, 10 Jan 1996 08:11:19
  11.     GMT
  12.  
  13. In article <4cvsm2$5ig@dub-news-svc-4.compuserve.com> 100754.2730@compuserve.com (Martin Aupperle) writes:
  14.  
  15. : Borland C++ V4.5 does not allow to return a reference to a local
  16. : variable:
  17.  
  18. : int &doIt() {
  19.  
  20. :   int i = 7;
  21. :   return i;  // syntax error
  22. :   }
  23.  
  24. : I remember that I once had a compiler that did allow it (it gave me
  25. : only a warning). 
  26. : Which one is right? I think that it should be an error because after
  27. : the function has terminated, the reference has no data object it is
  28. : bound to any more.
  29.  
  30. Many compilers will allow this, though you are correct it will refer to
  31. data which has been cleaned off the stack.
  32. Many compilers will also allow:
  33.  
  34.   int*
  35.   doIt(void)
  36.   {
  37.       int i = 7;
  38.       return &i;
  39.   }
  40.  
  41. Which suffers from the same problem.
  42. Both compilers are right. It is not required by the standard that the
  43. compiler issue diagnostic messages for this construct but it is not
  44. non-conforming (double-negatives are groovy) for issuing such messages.
  45.  
  46. Regards
  47.  
  48.     -A.
  49. -- 
  50. | A.Champion                |
  51.